/*
 * Copyright (c) 2017. 杭州端点网络科技有限公司.  All rights reserved.
 */

package com.jiantong.user.impl.service;

import com.google.common.base.Optional;
import com.google.common.base.Throwables;
import com.google.common.collect.Maps;
import com.jiantong.common.util.UserUtil;
import com.jiantong.user.ext.LoginUser;
import com.jiantong.user.impl.dao.OrganizationUserDao;
import com.jiantong.user.model.OrganizationUser;
import com.jiantong.user.service.OrganizationUserReadService;
import io.terminus.boot.rpc.common.annotation.RpcProvider;
import io.terminus.common.model.PageInfo;
import io.terminus.common.model.Paging;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author : panxin
 */
@Slf4j
@Service
@RpcProvider
public class OrganizationUserReadServiceImpl implements OrganizationUserReadService {

    private final OrganizationUserDao organizationUserDao;

    @Autowired
    public OrganizationUserReadServiceImpl(OrganizationUserDao organizationUserDao) {
        this.organizationUserDao = organizationUserDao;
    }

    @Override
    public Response<OrganizationUser> findOrganizationUserByUserId(Long userId) {
        try {
            OrganizationUser user = organizationUserDao.findByUserId(userId);
            if (user == null) {
                log.error("organization user is not exists, id = {}", userId);
                return Response.fail("user.not.exists");
            }
            return Response.ok(user);
        } catch (Exception e) {
            log.error("failed to find organization user by userId = {}, cause : {}",
                    userId, Throwables.getStackTraceAsString(e));
            return Response.fail("find.organization.user.failed");
        }
    }

    @Override
    public Response<Optional<OrganizationUser>> findOptionalOrganizationUserByUserId(Long userId) {
        try {
            return Response.ok(Optional.fromNullable(organizationUserDao.findByUserId(userId)));
        } catch (Exception e) {
            log.error("failed to find organization user by userId = {}, cause : {}",
                    userId, Throwables.getStackTraceAsString(e));
            return Response.fail("find.organization.user.failed");
        }
    }

    @Override
    public Response<Paging<OrganizationUser>> pagingGOVStaff(Integer pageNo, Integer pageSize) {
        try{
            PageInfo pageInfo = new PageInfo(pageNo, pageSize);
            Map<String , Object> criteria = Maps.newHashMap();
            LoginUser loginUser = UserUtil.getCurrentUser();
            Long organizationId = loginUser.getOrganizationId();
            criteria.put("organizationId",organizationId);
            return Response.ok(organizationUserDao.pagingGOVStaff(pageInfo.getOffset(),pageInfo.getLimit(),criteria));
        }catch (Exception e){
            log.error("failed to paging organization GOV user by userId = {}, cause : {}",
                    Throwables.getStackTraceAsString(e));
            return Response.fail("paging.organization.GOV.user.failed");
        }
    }

    @Override
    public Response<Paging<OrganizationUser>> pagingFBStaff(Integer pageNo, Integer pageSize) {
        try{
            PageInfo pageInfo = new PageInfo(pageNo, pageSize);
            Map<String , Object> criteria = Maps.newHashMap();
            LoginUser loginUser = UserUtil.getCurrentUser();
            Long organizationId = loginUser.getOrganizationId();
            criteria.put("organizationId",organizationId);
            return Response.ok(organizationUserDao.pagingFBStaff(pageInfo.getOffset(),pageInfo.getLimit(),criteria));
        }catch (Exception e){
            log.error("failed to paging organization FB user by userId = {}, cause : {}",
                    Throwables.getStackTraceAsString(e));
            return Response.fail("paging.organization.FB.user.failed");
        }
    }

    /**
     * 条件查询user_id
     * @param criteria
     * @return
     */
    @Override
    public Response<List<Long>> selectUserIds(Map criteria){
        try {
           if(criteria==null){
               criteria = new HashMap();
           }
           return Response.ok(organizationUserDao.selectUserIds(criteria));
        } catch (Exception e) {
            log.error("failed to find organization user by userId = {}, cause : {}",
                     Throwables.getStackTraceAsString(e));
            return Response.fail("find.organization.user.failed");
        }
    }


    /**
     * 条件查询
     * @param criteria
     * @return
     */
    @Override
    public Response<List<OrganizationUser>> selectList(Map criteria){
        try {
            if(criteria==null){
                criteria = new HashMap();
            }
            return Response.ok(organizationUserDao.selectList(criteria));
        } catch (Exception e) {
            log.error("failed to find organization user by userId = {}, cause : {}",
                    Throwables.getStackTraceAsString(e));
            return Response.fail("find.organization.user.failed");
        }
    }

    @Override
    public Response<List<String>> selectorgan() {
        try {
            return Response.ok(organizationUserDao.selectorgan());
        } catch (Exception e) {
            log.error("failed to find organization user by userId = {}, cause : {}",
                    Throwables.getStackTraceAsString(e));
            return Response.fail("find.organization.user.failed");
        }
    }
}
